<?php
class ModelReportOnline extends Model {
	public function getCustomersOnline($filter = null) {
		$sql = "SELECT co.*, CONCAT(c.firstname, ' ', c.lastname) as customer FROM  @@customer_online co
				LEFT JOIN  @@customer c ON (co.customer_id = c.customer_id) WHERE 1";

		
		if ($filter) {
			if ($filter->get('filter_ip')) {
				$sql .= "co.ip LIKE '" . ES($filter->get('filter_ip')) . "'";
			}
			if ($filter->get('filter_customer')) {
				$sql .= "co.customer_id > 0 AND CONCAT(c.firstname, '', c.lastname) LIKE '" . ES($filter->get('filter_customer')) . "'";
			}
			$sql .= $filter->sort('co.date_added', 'DESC');
			$sql .= $filter->limit();	
		}
		else {
			$sql .= " ORDER BY co.date_added DESC";
		}
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalCustomersOnline($filter = null) {
		$sql = "SELECT COUNT(*) AS total FROM `@@customer_online` co LEFT JOIN  @@customer c
				ON (co.customer_id = c.customer_id) WHERE 1";

		if ($filter) {
			if ($filter->get('filter_ip')) {
				$sql .= "co.ip LIKE '" . ES($filter->get('filter_ip')) . "'";
			}
			if ($filter->get('filter_customer')) {
				$sql .= "co.customer_id > 0 AND CONCAT(c.firstname, '', c.lastname) LIKE '" . ES($filter->get('filter_customer')) . "'";
			}
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}
}
?>